﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM
{
    public class Protected : IProtected
    {
        private string m_password;
        private bool m_is_protected;
        private bool m_protectable;

        public Protected()
        {
            m_password = "";
            m_is_protected = false;
            m_protectable = true;
        }

        public bool login(string password)
        {
            if (m_is_protected && m_protectable)
                return Hash.Sha512(password) == m_password;
            return true;
        }

        public bool protect(string password)
        {
            if (m_protectable)
            {
                if (!m_is_protected)
                {
                    m_is_protected = true;
                    m_password = Hash.Sha512(password);
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

        public rvalue deletePassword(string password)
        {
            if (m_protectable)
            {
                if (m_is_protected)
                {
                    if (Hash.Sha512(password) == m_password)
                    {
                        m_password = "";
                        m_is_protected = false;
                        return rvalue.PASSWORD_CLEARED;
                    }
                    else
                        return rvalue.BAD_PASSWORD;
                }

                return rvalue.NO_PASSWORD_FOUND;
            }
            else
                return rvalue.NOT_PROTECTABLE;
        }

        public rvalue modifyPassword(string old_password, string password)
        {
            if (m_protectable)
            {
                if (m_is_protected)
                {
                    if (Hash.Sha512(password) == m_password)
                    {
                        m_password = "";
                        m_is_protected = false;
                        return rvalue.PASSWORD_MODIFIED;
                    }
                    else
                        return rvalue.BAD_PASSWORD;
                }

                return rvalue.NO_PASSWORD_FOUND;
            }
            else
                return rvalue.NOT_PROTECTABLE;
        }

        public rvalue superUnprotect()
        {
            if (this.m_protectable)
            {
                if (this.m_is_protected)
                {
                    this.m_is_protected = false;
                    this.m_password = "";

                    return rvalue.PASSWORD_CLEARED;
                }
                else
                    return rvalue.NO_PASSWORD_FOUND;
            }
            else
                return rvalue.NOT_PROTECTABLE;
        }

        public rvalue superModifyPassword(string new_password)
        {
            if (this.m_protectable)
            {
                if (this.m_is_protected)
                {
                    this.m_is_protected = false;
                    this.m_password = "";
                    return rvalue.PASSWORD_MODIFIED;
                }
                else
                    return rvalue.NOT_PROTECTED;
            }
            else
                return rvalue.NOT_PROTECTABLE;
        }

        public void superSetProtectable(bool is_protectable)
        {
            if (this.m_is_protected && !is_protectable)
            {
                this.m_is_protected = false;
                this.m_password = "";
            }

            this.m_protectable = is_protectable;
        }

        public bool isProtected()
        {
            return this.m_is_protected;
        }

        public bool isProtectable()
        {
            return this.m_protectable;
        }
    }
}
